<?php
namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Employee;
use AuctionCore\AntNest\Sms;
use AuctionCore\System\AuctionException;
use AuctionCore\Fours\Fours;
use AuctionCore\Common\Page;
use AuctionCore\System\SystemLog;
use AuctionCore\Common\Util;

/**
 * 4s店
 */
class FoursController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
        //分公司
        $bc_list = $this->get_branch_company_list();
        $this->assign('bc_list', $bc_list);
    }


    public function index()
    {
        $this->display();
    }

    /**
     * 4s店/集团账号列表
     */
    public function get_fours_list()
    {
        $p = I('request.p/d', 1);
        $p = $p < 2 ? 1 : $p;
        $limit = 10;
        //搜索条件
        // 登陆权限下的人员对应城市
        // $bc_city = $this->bc_city;
        // $city = array();
        // foreach ($bc_city as $key => $value) {
        //     $city[] = $key;
        // }
        $params['status'] = I('get.status', '');
        $params['search_key'] = I('get.search_key', '');
        // $params['city'] = $city;

        $du = new Fours();
        if ($params['status'] == 1) {
            $result = $du->getBlocList($p, $limit, $params);
        } elseif ($params['status'] == 2) {
            $result = $du->getFoursList($p, $limit, $params);
        }
        //获取所有4s店信息
        $allfours = $du->getAllFours();
        //获取所有集团信息
        $allinfo = $du->getAllBloc();
        $list = empty($result['list']) ?: $result['list'];
        $this->assign('list', $list);
        $this->assign('params', $params);
        $this->assign('allfours', $allfours);
        $this->assign('allinfo', $allinfo);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $limit, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();
        //模板赋值
        $this->assign('page', $show);
        $this->display('get_fours_list');
    }


    public function get_city()
    {
        $city_id = I("get.city_id/d", 0);
        if ($city_id < 1) {
            $this->helpJsonResult(500, '城市id不能为空');
        }
        $du = new Fours();
        $fours = $du->getFoursCity(array($city_id));
        if (empty($fours)) {
            $this->helpJsonResult(500, 'empty');
        }
        $this->helpJsonResult(200, 'empty', $fours);
    }


    //添加4s店账号信息
    public function add_fours()
    {
        $user = array();
        $fu = array();
        //验证4s店
        $fu['f_id'] = trim(I('post.f_id'));
        if ($fu['f_id'] < 1) {
            $this->helpJsonResult(400, "4s店不能为空");
        }
        //验证用户名
        $user['user_name'] = trim(I('post.user_name/s'));
        if (empty($user['user_name'])) {
            $this->helpJsonResult(400, "用户名不能为空");
        }
        $is_exist = M('user')->where('user_name=\'' . $user['user_name'] . '\'')->find();
        if (!empty($is_exist)) {
            $this->helpJsonResult(403, "用户名已存在");
        }
        //随机生成pwd_mobile  数据库索引唯一
        $user['pwd_mobile'] = "4s" . time();
        //随机生成6位用户密码
        $passwd = rand(100000, 999999);
        $user['passwd'] = md5($passwd);
        //验证联系人
        $fu['real_name'] = trim(I('post.real_name'));
        if (empty($fu['real_name'])) {
            $this->helpJsonResult(400, "联系人不能为空");
        }
        //验证电话
        $fu['phone'] = trim(I('post.phone'));
        if (empty($fu['phone'])) {
            $this->helpJsonResult(400, "联系电话不能为空");
        }
        if (!preg_match("/^1[34578]{1}\d{9}$/", $fu['phone'])) {
            $this->helpJsonResult(400, "电话格式不正确");
        }
        //用户信息
        $user['reg_date'] = date("Y-m-d H:i:s", time());
        $res = Fours::create($user, $fu); //新增
        if ($res === false) {
            $this->helpJsonResult(500, "添加失败,服务器错误");
        }

        $sms = new Sms();
        $data = array('realname' => $fu['real_name'], 'username' => $user['user_name'], 'password' => $passwd);
        $res = $sms->notify(18, $fu['phone'], $data);
        if (!$res) {
            $this->helpJsonResult(400, "发送短信失败");
        }
        SystemLog::add(array(
            'emp_id' => $this->user_info['emp_id'],
            'action' => "新增4s店账号" . $user['user_name'] . " - " . $res
        ));
        $this->helpJsonResult(200, "添加成功");
    }

    //修改4s信息
    public function fours_eidt()
    {
        $uid = I('get.uid');
        if ($uid < 1) {
            $this->helpJsRedirect("用户id有误");
        }
        $bc_city = $this->bc_city;
        $city = array();
        foreach ($bc_city as $key => $value) {
            $city[] = $key;
        }
        $fours = new Fours();
        $info = $fours->getOneFours($uid);
        $allfours = $fours->getFoursCity(array($info['city']));
        $this->assign('info', $info);
        $this->assign('allfours', $allfours);
        $this->display();
    }

    //4s店保存修改
    public function fours_post()
    {
        $user = array();
        $fu = array();
        $user['uid'] = trim(I('get.uid'));
        if ($user['uid'] < 1) {
            $this->helpJsonResult(400, "用户id不能为空");
        }
        // $fu['f_id'] = trim(I('post.fours'));
        // if ($fu['f_id'] <1 ) {
        //     $this->helpJsonResult(400, "4s店不能为空");
        // }
        //验证用户名
        // $user['user_name'] = trim(I('post.user_name/s'));
        // if (empty($user['user_name'])) {
        //     $this->helpJsonResult(400, "用户名不能为空");
        // }
        // $is_exist = M('user')->where("user_name='$user[user_name]' and uid !='$user[uid]'")->find();
        // if (!empty($is_exist)) {
        //     $this->helpJsonResult(403, "用户名已存在");
        // }
        //验证联系人
        $fu['real_name'] = trim(I('post.real_name'));
        if (empty($fu['real_name'])) {
            $this->helpJsonResult(400, "联系人不能为空");
        }
        //验证4s店电话
        $fu['phone'] = trim(I('post.phone'));
        if (empty($fu['phone'])) {
            $this->helpJsonResult(400, "联系电话不能为空");
        }
        $user['last_modify_time'] = date('Y-m-d H:i:s');
        $fu['modifytime'] = date('Y-m-d H:i:s');
        $res = Fours::update($user, $fu); //新增
        if ($res === false) {
            $this->helpJsonResult(500, "修改失败,服务器错误");
        }
        $this->helpJsonResult(200, "修改成功");
    }
    // //4s店查看
    // public function fours_info()
    // {
    //     $uid = I('get.uid');
    //     if($uid<1){
    //         $this->helpJsRedirect("用户id有误");
    //     }
    //     $fours = new Fours();
    //     //获取4s店信息
    //     $info = $fours->get_one_fours($uid);
    //     $this->assign('info', $info);
    //     $this->display();
    // }
    //停用启用
    public function ci_op()
    {
        try {
            $uid = I('post.uid/d');
            $op_type = I('post.op_type');
            if (!$uid) {
                AuctionException::make_throw(1000001);
            }

            $f = new fours();
            if ($op_type == '1') {
                $res = $f->disable($uid);
            } elseif ($op_type === '0') {
                $res = $f->enable($uid);
            } else {
                AuctionException::make_throw('100067');
            }
            if ($res) {
                echo Util::auto_jsonp_or_json($_REQUEST, array('code' => 200, 'msg' => '', 'data' => ''));
                exit;
            } else {
                echo Util::auto_jsonp_or_json($_REQUEST, array('code' => 401, 'msg' => '更新失败', 'data' => ''));
                exit;
            }
        } catch (AuctionException $e) {
            echo Util::auto_jsonp_or_json($_REQUEST, array('msg' => $e->getMessage(), 'code' => $e->getCode()));
            exit;
        }
    }



    // //查看集团
    // public function bloc_info()
    // {
    //     $uid = I('get.uid');
    //     if($uid<1){
    //         $this->helpJsRedirect("集团id有误");
    //     }
    //     $fours = new Fours();
    //     //获取集团信息
    //     $bloc = $fours->get_one_bloc($uid);

    //     //获取集团下属4s信息
    //     $fours = $fours->get_bloc_fours($bloc['bid']);
    //     $this->assign('fours', $fours);
    //     $this->assign('bloc', $bloc);
    //     $this->display();
    // }
    //添加集团信息
    public function add_bloc()
    {
        $user = array();
        $bu = array();
        $bu['b_id'] = trim(I('post.b_id'));
        if ($bu['b_id'] < 1) {
            $this->helpJsonResult(400, "所属集团不能为空");
        }
        //验证用户名
        $user['user_name'] = trim(I('post.user_name'));
        if (empty($user['user_name'])) {
            $this->helpJsonResult(400, "用户名不能为空");
        }
        $is_exist = M('user')->where('user_name=\'' . $user['user_name'] . '\'')->find();
        if (!empty($is_exist)) {
            $this->helpJsonResult(403, "用户名已存在");
        }
        //随机生成pwd_mobile  数据库索引唯一
        $user['pwd_mobile'] = "bloc" . time();
        $passwd = rand(100000, 999999);
        //随机生成6位用户密码
        $user['passwd'] = md5($passwd);
        //验证联系人
        $bu['real_name'] = trim(I('post.real_name'));
        if (empty($bu['real_name'])) {
            $this->helpJsonResult(400, "姓名不能为空");
        }
        //验证电话
        $bu['phone'] = trim(I('post.phone'));
        if (empty($bu['phone'])) {
            $this->helpJsonResult(400, "联系电话不能为空");
        }
        if (!preg_match("/^1[34578]{1}\d{9}$/", $bu['phone'])) {
            $this->helpJsonResult(400, "电话格式不正确");
        }
        //用户信息
        $user['reg_date'] = date("Y-m-d H:i:s", time());
        $res = Fours::createBloc($user, $bu); //新增
        if ($res === false) {
            $this->helpJsonResult(500, "添加失败,服务器错误");
        }
        $sms = new Sms();
        $data = array('realname' => $bu['real_name'], 'username' => $user['user_name'], 'password' => $passwd);
        $res = $sms->notify(19, $bu['phone'], $data);
        if (!$res) {
            $this->helpJsonResult(200, "发送短信失败");
        }
        SystemLog::add(array(
            'emp_id' => $this->user_info['emp_id'],
            'action' => "新增集团账号" . $user['user_name'] . " - " . $res
        ));
        $this->helpJsonResult(200, "添加成功");
    }

    //修改集团账号信息
    public function bloc_eidt()
    {
        $uid = I('get.uid');
        if ($uid < 1) {
            $this->helpJsRedirect("用户id有误");
        }
        $fours = new Fours();
        $info = $fours->getOneBloc($uid);
        //获取所有集团
        $allbloc = $fours->getAllBloc();
        $this->assign('allbloc', $allbloc);
        $this->assign('info', $info);
        $this->display();

    }

    //修改集团账号信息提交
    public function bloc_post()
    {
        $user = array();
        $bu = array();
        $user['uid'] = trim(I('get.uid'));
        if ($user['uid'] < 1) {
            $this->helpJsonResult(400, "用户id不能为空");
        }
        // $bu['b_id'] = trim(I('post.b_id'));
        // if ($bu['b_id'] <1 ) {
        //     $this->helpJsonResult(400, "集团不能为空");
        // }
        //验证用户名
        // $user['user_name'] = trim(I('post.user_name/s'));
        // if (empty($user['user_name'])) {
        //     $this->helpJsonResult(400, "用户名不能为空");
        // }
        // $is_exist = M('user')->where("user_name='$user[user_name]' and uid !='$user[uid]'")->find();
        // if (!empty($is_exist)) {
        //     $this->helpJsonResult(403, "用户名已存在");
        // }
        //验证联系人
        $bu['real_name'] = trim(I('post.real_name'));
        if (empty($bu['real_name'])) {
            $this->helpJsonResult(400, "联系人不能为空");
        }
        //验证电话
        $bu['phone'] = trim(I('post.phone'));
        if (empty($bu['phone'])) {
            $this->helpJsonResult(400, "联系电话不能为空");
        }
        $user['last_modify_time'] = date('Y-m-d H:i:s');
        $bu['modifytime'] = date('Y-m-d H:i:s');
        $res = Fours::updateBloc($user, $bu); //修改
        if ($res === false) {
            $this->helpJsonResult(500, "修改失败,服务器错误");
        }
        $this->helpJsonResult(200, "修改成功");
    }

    //重置密码
    public function restpwd()
    {
        try {
            $uid = $_POST['uid'];
            $bu = new fours();
            $bu->restPwd($uid);
            echo Util::auto_jsonp_or_json($_REQUEST, array('code' => 200, 'msg' => '操作成功', 'data' => ''));
            exit;
        } catch (AuctionException $e) {
            echo Util::auto_jsonp_or_json($_REQUEST, array('msg' => $e->getMessage(), 'code' => $e->getCode()));
            exit;
        }
    }


    /***********************************************集团及4S店管理**********************************************************/


    public function fbmanger()
    {
        $is_hq = $this->is_hq;
        $bc_city = $this->bc_city;
        $this->display();
    }


    public function get_fbmanger_list()
    {
        $p = I('request.p/d', 1);
        $p = $p < 2 ? 1 : $p;
        $limit = 10;
        //搜索条件
        $params['status'] = I('get.status', '');
        $params['search_key'] = trim(I('get.search_key', ''));

        $rules = "4SGuanLiChaKanSuoYou";
        $e = new Employee($this->user_info['username']);
        $auth = $e->check_auth($rules);
        if (!$auth) {
            $params['user_id'] = $this->emp_id;
        }
        $citys = $this->bc_city;
        foreach ($citys as $key => $value) {
            $city_code[] = $key;
        }
        $bc_city = I("get.bc_city");
        if ($bc_city) {
            $params['bc_city'] = $bc_city;
        } else {
            $params['bc_city'] = $city_code;
        }
        // $is_hq = $this->is_hq;
        // $bc_city = $this->bc_city;
        // if (!$is_hq) {
        //     foreach ($bc_city as $key => $value) {
        //         $all_city[] = $key;
        //     }
        //     $params['bc_city'] = $all_city;
        // }
        $du = new Fours();
        if ($params['status'] == 1) {
            $result = $du->get_bmanger_list($p, $limit, $params);
        } elseif ($params['status'] == 2) {
            $params['bid'] = I('get.bid/d', '');
            $result = $du->get_fmanger_list($p, $limit, $params);
        }
        //获取所有集团信息  
        $allbloc = $du->getAllBloc();
        $list = empty($result['list']) ?: $result['list'];
        $rules = "4SGuanLiBianJiSuoYou";
        $e = new Employee($this->user_info['username']);
        $edit_auth = $e->check_auth($rules);
        $this->assign('edit_auth', $edit_auth);
        //$this->assign('bc_id', $bc_id);
        $this->assign('list', $list);
        $this->assign('params', $params);
        //$this->assign('allfours', $allfours);
        $this->assign('allbloc', $allbloc);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $limit, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();
        //模板赋值
        $this->assign('page', $show);
        $this->display();
    }

    //添加集团
    public function add_bm()
    {
        $bloc = array();
        //验证集团名称
        $bloc['b_name'] = trim(I('post.b_name'));
        $bloc['addr'] = trim(I('post.addr'));
        $bloc['contact'] = trim(I('post.contact'));
        $bloc['phone'] = trim(I('post.phone'));
        if (empty($bloc['b_name'])) {
            $this->helpJsonResult(400, "集团名称不能为空");
        }
        $is_exist = M('bloc')->where('b_name=\'' . $bloc['b_name'] . '\'')->find();
        if (!empty($is_exist)) {
            $this->helpJsonResult(403, "集团名称已存在");
        }
        $bloc['createtime'] = date("Y-m-d H:i:s", time());
        $bloc['modifytime'] = date("Y-m-d H:i:s", time());
        $fours = new Fours();
        $res = $fours->createbm($bloc); //新增
        if ($res < 1) {
            $this->helpJsonResult(500, "添加失败,服务器错误");
        }
        $this->helpJsonResult(200, "添加成功");
    }


    //通过分公司找对应4s店开发人
    public function get_shop_developer()
    {
        $branch_company = I("post.branch_company_id", 0);
        if ($branch_company < 1) {
            $this->helpJsonResult(200, '分公司信息错误');
        }
        $shop_developer_id = C('SHOP_DEVELOPER.0');
        $shop_developer = Employee::get_branch_emp($shop_developer_id, $branch_company);
        if (empty($shop_developer)) {
            $this->helpJsonResult(500, 'empty');
        }
        $this->helpJsonResult(200, 'empty', $shop_developer);
    }

    //添加4s店
    public function add_fm()
    {
        $bu = new Fours();

        $edit_rules = "4SGuanLiBianJiSuoYou";
        $e = new Employee($this->user_info['username']);
        $edit_auth = $e->check_auth($edit_rules);
        $this->assign('edit_auth', $edit_auth);

        if (IS_POST) {
            $fours = array();
            $come_from = array();
            $fours['f_name'] = trim(I('post.f_name'));
            $fours['addr'] = trim(I('post.addr'));
            $fours['contact'] = trim(I('post.contact'));
            $fours['phone'] = trim(I('post.phone'));
            // $fours['developer_bc_id'] = trim(I('post.branch_company'));
            $developer_id = I('post.developer/d', 0);
            if ($developer_id > 0) {
                $fours['developer'] = $developer_id;
            }
            $fours['b_id'] = trim(I('post.b_id'));
            $fours['city'] = trim(I('post.bc_city'));

            $bank_name = I('post.bank_name', '', 'trim');
            $card_no = I('post.card_no', '', 'trim');
            $account_name = I('post.account_name', '', 'trim');

            //验证4s名称
            if (empty($fours['f_name'])) {
                $this->helpJsonResult(400, "4S店名称不能为空");
            }
            if (empty($fours['city'])) {
                $this->helpJsonResult(400, "所属城市不能为空");
            }
            if (empty($developer_id)) {
                $this->helpJsonResult(400, "4s店开发人不能为空");
            }
            $is_exist = M('fours')->where('f_name=\'' . $fours['f_name'] . '\'')->find();
            if (!empty($is_exist)) {
                $this->helpJsonResult(403, "4S店名称已存在");
            }
            if (empty($bank_name)) {
                $this->helpJsonResult(400, "开户行不能为空");
            }
            if (empty($card_no)) {
                $this->helpJsonResult(400, "银行账号不能为空");
            }
            if (empty($account_name)) {
                $this->helpJsonResult(400, "账户名不能为空");
            }
            $bank_account = [];
            if ($edit_auth) {
                $bank_name_1 = I('post.bank_name_1', '', 'trim');
                $card_no_1 = I('post.card_no_1', '', 'trim');
                $account_name_1 = I('post.account_name_1', '', 'trim');
                if (!empty($bank_name_1) && !empty($card_no_1) && !empty($account_name_1)) {
                    $bank_account = [
                        [
                            'card_no' => $card_no,
                            'account_name' => $account_name,
                            'bank_name' => $bank_name
                        ],
                        [
                            'card_no' => $card_no_1,
                            'account_name' => $account_name_1,
                            'bank_name' => $bank_name_1
                        ]
                    ];
                } elseif (empty($bank_name_1) && empty($card_no_1) && empty($account_name_1)) {
                    $bank_account =
                        [
                            'card_no' => $card_no,
                            'account_name' => $account_name,
                            'bank_name' => $bank_name
                        ];
                } else {
                    $this->helpJsonResult(400, "银行账户信息不完整");
                }
            } else {
                //添加帐号信息
                $bank_account = [
                    'card_no' => $card_no,
                    'account_name' => $account_name,
                    'bank_name' => $bank_name
                ];
            }
            $fours['createtime'] = date("Y-m-d H:i:s", time());
            $fours['modifytime'] = date("Y-m-d H:i:s", time());
            //新增4s店需要往渠道表插入
            $come_from['pid'] = 5;
            $come_from['from_name'] = $fours['f_name'];
            $come_from['createtime'] = date("Y-m-d H:i:s", time());
            $res = $bu->createfm($fours, $bank_account, $come_from); //新增
            if ($res === false) {
                $this->helpJsonResult(500, "添加失败,服务器错误");
            }
            $this->helpJsonResult(200, "添加成功");
        }
        //获取所有集团信息  
        $allbloc = $bu->getAllBloc();
        $this->assign('allbloc', $allbloc);
        $this->display();
    }

    //集团管理-修改
    public function bm_eidt()
    {
        $bid = I('get.bid');
        if ($bid < 1) {
            $this->helpJsRedirect("集团id有误");
        }
        $fours = new Fours();
        if (IS_POST) {
            $bloc = array();
            $bloc['b_name'] = trim(I('post.b_name'));
            $bloc['addr'] = trim(I('post.addr'));
            $bloc['contact'] = trim(I('post.contact'));
            $bloc['phone'] = trim(I('post.phone'));
            $bloc['modifytime'] = date('Y-m-d H:i:s');
            if (empty($bloc['b_name'])) {
                $this->helpJsonResult(400, "集团名称不能为空");
            }
            $is_exist = M('bloc')->where("b_name='$bloc[b_name]' and bid !='$bid'")->find();
            if (!empty($is_exist)) {
                $this->helpJsonResult(403, "集团名称已存在");
            }
            $res = $fours->updatebm($bloc, $bid); //修改
            if ($res < 1) {
                $this->helpJsonResult(500, "修改失败,服务器错误");
            }
            $this->helpJsonResult(200, "修改成功");
        }
        $info = $fours->find_bloc($bid);
        $this->assign('info', $info);
        $this->display();
    }

    //4s管理-修改
    public function fm_eidt()
    {
        $fid = I('get.fid');
        if ($fid < 1) {
            $this->helpJsRedirect("4s店id有误");
        }
        $eidt_rules = "4SGuanLiBianJiSuoYou";
        $e = new Employee($this->user_info['username']);
        $eidt_auth = $e->check_auth($eidt_rules);
        $this->assign('eidt_auth', $eidt_auth);

        $Fours = new Fours();

        //获取4s店对应的所有打款账户
        $fours_bank_account_list = $Fours->get_bank_account_by_fid($fid);
        $fours_bank_account_count = count($fours_bank_account_list);
        if (IS_POST) {
            $fours = array();
            $fours['f_name'] = trim(I('post.f_name'));
            $fours['addr'] = trim(I('post.addr'));
            $fours['contact'] = trim(I('post.contact'));
            $fours['phone'] = trim(I('post.phone'));
            $fours['developer_bc_id'] = trim(I('post.branch_company'));
            $developer_id = I('post.developer/d', 0);
            if ($developer_id > 0) {
                $fours['developer'] = $developer_id;
            }
            $fours['b_id'] = trim(I('post.b_id'));
            $fours['city'] = trim(I('post.bc_city'));
            $fours['channel_id'] = trim(I('post.channel_id'));
            $fours['modifytime'] = date('Y-m-d H:i:s');
            if ($eidt_auth) {
                if ($fours_bank_account_count > 1) {
                    $fours_bank_account_data = [];
                    foreach ($fours_bank_account_list as $fk => $fv) {
                        $fours_bank_account_data[$fk]['id'] = $fv['id'];
                        $fours_bank_account_data[$fk]['card_no'] = trim($_POST['card_no_' . $fv['id']]);
                        $fours_bank_account_data[$fk]['bank_name'] = trim($_POST['bank_name_' . $fv['id']]);
                        $fours_bank_account_data[$fk]['account_name'] = trim($_POST['account_name_' . $fv['id']]);
                    }
                    if (empty($fours_bank_account_data[0]['card_no'])) {
                        $this->helpJsonResult(400, "银行账号不能为空");
                    }
                    if (empty($fours_bank_account_data[0]['bank_name'])) {
                        $this->helpJsonResult(400, "开户行不能为空");
                    }
                    if (empty($fours_bank_account_data[0]['account_name'])) {
                        $this->helpJsonResult(400, "账户名不能为空");
                    }
                    if (
                        !empty($fours_bank_account_data[1]['card_no']) &&
                        !empty($fours_bank_account_data[1]['bank_name']) &&
                        !empty($fours_bank_account_data[1]['account_name'])
                    ) {
                    } elseif (
                        empty($fours_bank_account_data[1]['card_no']) &&
                        empty($fours_bank_account_data[1]['bank_name']) &&
                        empty($fours_bank_account_data[1]['account_name'])
                    ) {
                    } else {
                        $this->helpJsonResult(400, "银行账户信息不完整");
                    }
                } else {
                    $account_id = trim($_POST['account_id_' . $fid]);
                    $fours['card_no'] = trim($_POST['card_no_' . $account_id]);
                    $fours['bank_name'] = trim($_POST['bank_name_' . $account_id]);
                    $fours['account_name'] = trim($_POST['account_name_' . $account_id]);

                    $bank_account_fid = [
                        'id' => $account_id,
                        'card_no' => $fours['card_no'],
                        'account_name' => $fours['account_name'],
                        'bank_name' => $fours['bank_name']
                    ];
                    $bank_account = [
                        'fid' => $fid,
                        'card_no' => trim($_POST['card_no']),
                        'account_name' => trim($_POST['account_name']),
                        'bank_name' => trim($_POST['bank_name']),
                    ];
                }
            }
            if (empty($developer_id)) {
                $this->helpJsonResult(400, "4s店开发人不能为空");
            }
            if (empty($fours['f_name'])) {
                $this->helpJsonResult(400, "4s店名称不能为空");
            }
            $is_exist = M('fours')->where("f_name='$fours[f_name]' and fid !='$fid'")->find();
            if (!empty($is_exist)) {
                $this->helpJsonResult(403, "4s店名称已存在");
            }
            if ($eidt_auth) {
                if ($fours_bank_account_count > 1) {
                    foreach ($fours_bank_account_data as $fk => $fv) {
                        $Fours->update_bank_acount($fv);
                    }
                } else {
                    if (empty($fours['bank_name'])) {
                        $this->helpJsonResult(400, "开户行不能为空");
                    }
                    if (empty($fours['card_no'])) {
                        $this->helpJsonResult(400, "银行账号不能为空");
                    }
                    if (empty($fours['account_name'])) {
                        $this->helpJsonResult(400, "账户名不能为空");
                    }
                    if (
                        !empty($bank_account['card_no']) &&
                        !empty($bank_account['bank_name']) &&
                        !empty($bank_account['account_name'])
                    ) {
                    } elseif (
                        empty($bank_account['card_no']) &&
                        empty($bank_account['bank_name']) &&
                        empty($bank_account['account_name'])
                    ) {
                    } else {
                        $this->helpJsonResult(400, "银行账户信息不完整");
                    }
                    $Fours->update_bank_acount($bank_account_fid);
                    if (!empty($_POST['card_no']) && !empty($_POST['account_name']) && !empty($_POST['bank_name'])) {
                        $Fours->add_bank_acount($bank_account);
                    }
                }
            }
            $res = $Fours->updatefm($fours, $fid); //修改
            if ($res === false) {
                $this->helpJsonResult(500, "修改失败,服务器错误");
            }
            $this->helpJsonResult(200, "修改成功");
        }
        $info = $Fours->find_fours($fid);
        if ($info) {
            $developer_bc_id = \AuctionCore\Account\Back\Employee::get_emp_branch_id($info['developer']);
            if ($developer_bc_id != $info['developer_bc_id']) {
                $info['developer'] = 0;
            }
        }
        //获取所有集团信息
        $allbloc = $Fours->getAllBloc();
        $this->assign('info', $info);
        $this->assign('allbloc', $allbloc);
        //4s店对应打款帐号
        $this->assign('fours_bank_account_count', $fours_bank_account_count);
        $this->assign('fours_bank_account_list', $fours_bank_account_list);
        $this->display();
    }


    //集团-添加4s店
    public function add_bf()
    {
        $bid = I('get.bid');
        if ($bid < 1) {
            $this->helpJsRedirect("集团id有误");
        }
        $fours = new Fours();
        if (IS_POST) {
            try {
                $myspm = array();
                $spm = $fours->get_spm($bid);
                foreach ($spm as $value) {
                    array_push($myspm, $value['fid']);
                }
                $post_spm = I('post.fours');

                //清空所有4S店
                if (empty($post_spm)) {
                    foreach ($myspm as $val) {
                        $fours->exgraft_spm($bid, $val);
                    }
                } else {
                    $add_spm = array_diff($post_spm, $myspm);
                    $del_spm = array_diff($myspm, $post_spm);
                    foreach ($add_spm as $v) {
                        $fours->set_spm($bid, $v);
                    }
                    foreach ($del_spm as $val) {
                        $fours->exgraft_spm($bid, $val);
                    }
                }
                $this->helpJsonResult(200, "保存成功");
            } catch (AuctionException $e) {
                $this->helpJsonResult($e->getCode(), $e->getMessage());
            }
        }
        //获取所有4s店信息
        $allfours = $fours->getAllFours();
        //获取该集团已经分配的4s店
        $myspm = array();
        $spm = $fours->get_spm($bid);

        foreach ($spm as $key => $value) {
            array_push($myspm, $value['fid']);
        }
        $this->assign('myspm', $myspm);
        $this->assign('bid', $bid);
        $this->assign('allfours', $allfours);
        $this->display();
    }
}